﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Waker.Common;

namespace Waker.Devices
{
    public partial class FormSafeEdit : Base.FormBasePop
    {
        DataTable _DtSource;
        int _ID;
        public FormSafeEdit(int id)
        {
            InitializeComponent();
            this.Text += id < 1 ? "添加" : "修改";
            _ID = id;
            this.Load += new EventHandler(FormSafeEdit_Load);
        }

        private void FormSafeEdit_Load(object sender, EventArgs e)
        {
            var bal = new BAL.BAL_Device();
            _DtSource = bal.GetSafeTimePeriodByID(_ID);
            lueDevice_ID.Properties.DataSource = bal.GetAllDeviceIDAndName();
            ControlUtil.FillLookUpEdit(bal.GetAllDeviceIDAndName(), lueDevice_ID, "Device_Name", "Device_ID");
            if (_DtSource.Rows.Count < 1)
            {
                this.BindingContext[_DtSource].AddNew();
                this.BindingContext[_DtSource].EndCurrentEdit();
                var dr = _DtSource.Rows[0];
                dr["BeginTime"] = dr["EndTime"] = dr["BeginDate"] = new DateTime(2000, 1, 1);
                dr["EndDate"] = new DateTime(2099, 1, 1);
            }
            var subID = string.Empty;
            if (_DtSource.Rows[0]["LSubDevice_ID"] != DBNull.Value)
                subID = _DtSource.Rows[0]["LSubDevice_ID"].ToString();
            ControlUtil.BindData(this.Controls, _DtSource);
            icbLSubDevice_ID.EditValue = subID;
        }
        protected override void FormSave()
        {
            if (_DtSource == null || _DtSource.Rows.Count < 1)
                return;
            base.errorPop.ClearErrors();
            if (lueDevice_ID.EditValue == null || lueDevice_ID.EditValue == DBNull.Value)
            {
                base.errorPop.SetError(lueDevice_ID, "请选择设备");
                return;
            }
            if (icbLSubDevice_ID.EditValue == null || icbLSubDevice_ID.EditValue == DBNull.Value)
            {
                base.errorPop.SetError(icbLSubDevice_ID, "请选择门");
                return;
            }
            if (string.IsNullOrEmpty(txtSafeName.Text))
            {
                base.errorPop.SetError(txtSafeName, "名称不能为空");
                return;
            }
            if (cbeSafeWeek.EditValue == null || cbeSafeWeek.EditValue == DBNull.Value)
            {
                base.errorPop.SetError(cbeSafeWeek, "请选择星期");
                return;
            }
            if (dteBeginDate.EditValue == null || dteBeginDate.EditValue == DBNull.Value)
            {
                base.errorPop.SetError(dteBeginDate, "请选择开始日期");
                return;
            }
            if (dteEndDate.EditValue == null || dteEndDate.EditValue == DBNull.Value)
            {
                base.errorPop.SetError(dteEndDate, "请选择结束日期");
                return;
            }
            if (tetBeginTime.EditValue == null || tetBeginTime.EditValue == DBNull.Value)
            {
                base.errorPop.SetError(tetBeginTime, "请选开始时间");
                return;
            }
            if (tetEndTime.EditValue == null || tetEndTime.EditValue == DBNull.Value || tetEndTime.Time.ToLongTimeString() == "00:00:00")
            {
                base.errorPop.SetError(tetEndTime, "请选结束时间");
                return;
            }
            if (tetEndTime.Time < tetBeginTime.Time)
            {
                base.errorPop.SetError(tetEndTime, "结束时间不能小于开始时间");
                return;
            }
            this.BindingContext[_DtSource].EndCurrentEdit();
            if (_DtSource == null || _DtSource.Rows.Count < 1)
                return;

            var isOk = new BAL.BAL_Device().SaveSafeTimePeriod(_DtSource.Rows[0]);
            if (isOk)
            {
                UI.ShowInfo("保存成功");
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                UI.ShowError("保存失败");
            }
        }

        private void lueDevice_ID_EditValueChanged(object sender, EventArgs e)
        {
            if (lueDevice_ID.EditValue == null || lueDevice_ID.EditValue == DBNull.Value || string.IsNullOrEmpty(lueDevice_ID.EditValue.ToString()))
                return;
            var dt = new BAL.BAL_Device().GetSubDeviceByDeviceID(lueDevice_ID.EditValue.ToString());
            icbLSubDevice_ID.Properties.Items.Clear();
            ControlUtil.FillImageComboBox(dt, icbLSubDevice_ID, "LSubDevice_Name", "LSubDevice_ID");
            //icbLSubDevice_ID.SelectedIndex = 0;
        }

    }
}
